package com.arpa.oms.controller;

import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.controller.BaseController;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.oms.domain.dto.OmsSupplierDTO;
import com.arpa.oms.domain.dto.OmsSupplierFormDTO;
import com.arpa.oms.domain.entity.OmsSupplier;
import com.arpa.oms.domain.vo.OmsSupplierVO;
import com.arpa.oms.service.IOmsSupplierService;
import com.arpa.wms.domain.consts.NumConst;
import com.arpa.wms.domain.vo.SupplierVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.arpa.wms.domain.consts.TipConst.CODE_NOT_EMPTY;

/**
 * <p>
 * OMS供应商 Controller
 * </p>
 *
 * @author wangdong
 * @since 2021-07-21
 */
@Api(tags = "OMS供应商管理")
@RestController
@RequestMapping(value = "/omsSupplier", produces = "application/json;charset=UTF-8")
@Log4j2
public class OmsSupplierController extends BaseController {

    private final IOmsSupplierService iOmsSupplierService;

    public OmsSupplierController(IOmsSupplierService iOmsSupplierService) {
        this.iOmsSupplierService = iOmsSupplierService;
    }

    /**
     * 获取 供应商 列表
     *
     * @param supplierDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 供应商 列表", notes = "获取 供应商 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = SupplierVO.class))
    @NeedLogin
    @GetMapping("/list")
    public Result list(OmsSupplierDTO supplierDTO, HttpServletRequest request) {
        try {
            supplierDTO.setShipmentCode(UserUtil.getLoginWarehouseCodeOrShipmentCode());
            OmsSupplierVO supplierVOSum = iOmsSupplierService.queryListSum(supplierDTO);
            List<OmsSupplierVO> supplierVOList = iOmsSupplierService.queryList(supplierDTO);
            Page returnPage = new Page(supplierDTO.getPageNum(), supplierDTO.getPageSize());
            returnPage.setRecords(supplierVOList);
            returnPage.setTotal(supplierVOSum.getTotal());
            returnPage.setSumObject(supplierVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation(value = "模糊查询供应商", notes = "模糊查询供应商")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "keywords", value = "供应商名称、编号、电话", paramType = "path", dataType = "String")
    })
    @NeedLogin
    @GetMapping(value = "/search")
    public Result search(@RequestParam(value = "keywords", defaultValue = "") String keywords, HttpServletRequest request) {
        try {
            List<Map<String, Object>> result = iOmsSupplierService.search(keywords);
            return Result.ok().data(result);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 供应商
     *
     * @param entity  OmsSupplier
     * @param request
     * @returns
     */
    @ApiOperation(value = "添加 供应商 ", notes = "添加 供应商 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping("/save")
    public Result create(@RequestBody @Validated(OmsSupplierFormDTO.Save.class) OmsSupplierFormDTO entity, HttpServletRequest request) {
        try {
            iOmsSupplierService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取供应商详情
     *
     * @param code
     * @param request
     * @return
     */
    @ApiOperation(value = "获取供应商详情", notes = "获取供应商详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "code", value = "供应商code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = OmsSupplier.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result supplier(@PathVariable String code, HttpServletRequest request) {
        try {
            OmsSupplierVO supplierVO = iOmsSupplierService.getOneInfo(code);
            return Result.ok().data(supplierVO);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改供应商
     *
     * @param entity  供应商
     * @param request
     * @return
     */
    @ApiOperation(value = "修改供应商", notes = "修改供应商")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(@RequestBody @Validated(OmsSupplierFormDTO.Update.class) OmsSupplierFormDTO entity, HttpServletRequest request) {
        if (StringUtils.isBlank(entity.getCode())) {
            return Result.error(CODE_NOT_EMPTY);
        }
        int updateNum = iOmsSupplierService.updateByCode(entity);
        if (updateNum == 0) {
            return Result.error("没有更新数据！code:" + entity.getCode());
        } else {
            return Result.ok("修改成功,更新" + updateNum + "条数据！");
        }
    }


    /**
     * 删除 供应商
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 供应商 ", notes = "删除 供应商 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "codes", value = "codes", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "/batchdel")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iOmsSupplierService.remove(new QueryWrapper<OmsSupplier>().lambda().in(OmsSupplier::getCode, codes));
            return Result.ok();
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation(value = "OMS获取供应商下拉数据", notes = "OMS获取当前入驻商审核通过的供应商下拉数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @GetMapping(value = "/chooseData")
    public Result chooseData(@RequestParam(value = "keywords", defaultValue = "") String keywords, HttpServletRequest request) {
        List<OmsSupplier> omsSuppliers = iOmsSupplierService.list(new LambdaQueryWrapper<OmsSupplier>()
                .select(OmsSupplier::getCode, OmsSupplier::getName)
                .eq(OmsSupplier::getShipmentCode, UserUtil.getLoginWarehouseCodeOrShipmentCode())
                .eq(OmsSupplier::getStatus, NumConst.NUM_ONE)
        );
        return Result.ok().data(omsSuppliers);
    }

}
